package com.xxl.javaWork_15;

import java.io.*;

/**
 * @Author: xiaxinlin
 * @Date: 2020/12/22 22:01
 */
public class StreamDemo {
    public static void main(String[] args) {
        File file = new File("D:\\img\\test\\test.txt");
        try {
            readFromConsoleTo(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void readFromConsoleTo(File daddr) throws Exception {
        File file = daddr.getParentFile();
        if (!file.exists()){
            file.mkdirs();
        }
        System.out.println("请输入文字，输入“exit”退出");
        //创建转换流，从控制台输入
        InputStreamReader reader = new InputStreamReader(System.in,"UTF-8");
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(daddr),"UTF-8");
        int len;
        char c[]= new char[1024];
        while ((len=reader.read(c))!=-1){
            if (new String(c,0,len-1).equals("exit")){
                break;
            }
            System.out.println(new String(c,0,len-1));
            writer.write(c,0,len);
            writer.flush();
        }
        reader.close();
        writer.close();

    }
}
